package be.nepherte.movmanager.client.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.lang.String;

/**
 * This class represents various functionality related to string manipulation.
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see <a
 * href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses</a>.
 * </p>
 * 
 * @author	Bart Verhoeven
 * @version	0.1
 */
public class StringUtil {

	/**
	 * Splits this string around matches of the given regular expression. Empty
	 * matches are ignored.
	 * 
	 * @param	text
	 *          the text to be split
	 * @param	regex
	 *          the delimiting regular expression
	 * @return	the array of strings computed by splitting this string around
	 *         	matches of the given regular expression
	 * @see		{@link String#split()}
	 */
	public static List<String> split(String text, String regex) {
		String[] split = text.split(regex);
		List<String> values = new ArrayList<String>();
		
		for (String value: split) {
			value = StringUtil.trimSpaces(value);
			
			if (!value.isEmpty())
				values.add(value);
		}
		
		return values;
	}
	
	/**
	 * Trims all leading spaces from a given text.
	 * 
	 * @param	text
	 * 			text to be trimmed
	 * @return	text with no leading spaces
	 */
	public static String trimLeadingSpaces(String text) {
		return text.replaceAll("^\\s+", "");
	}
	
	/**
	 * Trims all trailing spaces from a given text.
	 * 
	 * @param	text
	 * 			text to be trimmed
	 * @return	text with no trailing spaces
	 */
	public static String trimTrailingSpaces(String text) {
		return text.replaceAll("\\s+$", "");
	}
	
	/**
	 * Trims all leading and trailing spaces from a given text.
	 * 
	 * @param	text
	 * 			text to be trimmed
	 * @return	text with no leading and trailing spaces
	 */
	public static String trimSpaces(String text) {
		return StringUtil.trimLeadingSpaces(StringUtil.trimTrailingSpaces(text));
	}
	
	/**
	 * Creates a string by appending all the string representations of the
	 * elements in the given list separated by the given separator.
	 * 
	 * @param	list
	 *          the list of which you want to obtain a string
	 * @param	separator
	 *          the token by which the elements in the list have to be
	 *          separated
	 * @return	a string with all items from the list separated
	 */
	public static <T> String toString(Collection<T> list, String separator) {
		StringBuffer buffer = new StringBuffer();
		
		for (T item: list) {
			buffer.append(item.toString() + separator);
		}
		
		
		int bufferLength = buffer.length();
		int separatorLength = separator.length();
		
		try {
			buffer.delete(bufferLength-separatorLength, bufferLength);
		} catch (StringIndexOutOfBoundsException e) {
			// the given list was empty, delete separator.
			buffer.delete(0, separatorLength);
		}
		
		return buffer.toString();
	}
}
